package ling.common.domain;

import com.easy.query.core.annotation.ColumnIgnore;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import ling.common.domain.entity.IdEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Result<T> extends R<T> implements IdEntity.IExtendInfo {


    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;
    public static final int BAD = HttpStatus.BAD_REQUEST;
    public boolean isSuccess()
    {
        return R.SUCCESS == getCode();
    }
    public static <T> Result<T> bad()
    {
        return restResult(null, BAD, "请求错误");
    }

    public static  Result bad(String msg)
    {
        return restResult(null, BAD, msg);
    }
    public static <T> Result<T> bad(String msg,T data)
    {
        return restResult(data, BAD, msg);
    }
    public static <T> Result<T> restResult(T data, int code, String msg)
    {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
    public static <T> Result<T> restResult(boolean isOk,String msg,T data){
        Result<T> apiResult = new Result<>();
        apiResult.setCode(isOk?SUCCESS:FAIL);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
    public static <T> Result<T> ok()
    {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(T data)
    {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> fail()
    {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> Result<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }
    public static <T> ResultRows<T> toRows(long total, List<T> list){
        return  new ResultRows<>(total,list);
    }



    private final HashMap<String, Object> extendMap=new HashMap<>();
    @Override
    public Map<String,Object> getExtendMap(){
        return extendMap;
    }
}
